
// 创建store 仓库步骤

// 01: 引入创建仓库的方法
import { legacy_createStore as createStore } from 'redux';

// 定义state中的数据类型
interface stateType {
    count: number
}

interface actionType {
    type: string,
    payload?: number
}


// 02: 定义初始state 数据
const defaultState: stateType = {
    count: 0
}

// 03: 定义reducer 纯函数
const reducer = (state = defaultState, action: actionType) => {
    if (action.type == '+') {
        if (action.payload) {
            return {
                count: state.count + action.payload
            }
        } else {
            return {
                count: state.count + 10
            }
        }
    }

    return state
}


// 04: 使用createStore 方法创建store仓库
// 全局声明winow为any类型
declare const window: any;

const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())

//05:导出store仓库
export default store